/**
 * 通知消息事件管理器
 * Name = notifyEvent
 * DateTime = Sat Jan 15 2022 22:40:16 GMT+0800 (中国标准时间)
 * Author = Created by zhj9922
 * FileBasename = notifyEvent.ts
 * gitee： https://gitee.com/coyeking
 */

 import { sys, _decorator } from "cc";
 
 export class notifyEvent {
     private static _handlers: { [key: string]: any[] } = {};
 
     /**
      * 监听事件
      * @param {string} eventName 事件名称
      * @param {function} handler 监听函数
      * @param {object} target 监听目标
      */
     public static on(eventName: string, handler: Function, target: any) {
         var objHandler: {} = { handler: handler, target: target };
         var handlerList: Array<any> = notifyEvent._handlers[eventName];
         if (!handlerList) {
             handlerList = [];
             notifyEvent._handlers[eventName] = handlerList;
         }
 
         for (var i = 0; i < handlerList.length; i++) {
             if (!handlerList[i]) {
                 handlerList[i] = objHandler;
                 return i;
             }
         }
 
         handlerList.push(objHandler);
         return handlerList.length;
     };
 
     /**
      * 取消监听
      * @param {string} eventName 监听事件
      * @param {function} handler 监听函数
      * @param {object} target 监听目标
      */
     public static off(eventName: string, handler: Function, target: any) {
         console.log('-- platform --', sys.platform);
         var handlerList = notifyEvent._handlers[eventName];
 
         if (!handlerList) {
             return;
         }
 
         for (var i = 0; i < handlerList.length; i++) {
             var oldObj = handlerList[i];
             if (oldObj.handler === handler && (!target || target === oldObj.target)) {
                 handlerList.splice(i, 1);
                 break;
             }
         }
     };
 
     /**
      * 分发事件
      * @param {string} eventName 分发事件名
      * @param  {...any} params 分发事件参数
      */
     public static dispatchEvent(eventName: string, ...args: any) {
         var handlerList = notifyEvent._handlers[eventName];
 
         var args1 = [];
         var i;
         for (i = 1; i < arguments.length; i++) {
             args1.push(arguments[i]);
         }
 
         if (!handlerList) {
             return;
         }
 
         for (i = 0; i < handlerList.length; i++) {
             var objHandler = handlerList[i];
             if (objHandler.handler) {
                 objHandler.handler.apply(objHandler.target, args1);
             }
         }
     };
 }
 

